<template>
  <!-- 首先最外层是Main.vue中注释的<el-aside width="200px" class="el-aside">Aside</el-aside>，我们将侧边栏改成我们自定义的组件，通过common-aside引入我们自己写的组件 -->
  <el-aside :width="width" class="el-aside">
    <el-menu background-color="#545c64" text-color="#fff" :collapse="isCollapse" :collapse-transition="false">
      <!-- 写菜单栏的大标题————通用后台管理系统 -->
      <h3 v-show="!isCollapse">通用后台管理系统</h3>
      <h3 v-show="isCollapse">后台</h3>
      <!-- 循环遍历菜单栏 -->
      <!-- 1.没有子菜单的遍历 -->
      <el-menu-item @click="handleMenu(item)" v-for="item in noChildren" :key="item.path" :index="item.path">
        <!-- <el-icon><icon-menu /></el-icon> -->
        <!-- 动态获取icon图标 -->
        <component :is="item.icon" class="icons"></component>
        <!-- 显示文字内容 -->
        <span>{{ item.label }}</span>
      </el-menu-item>
      <!-- 2.有子菜单的遍历 -->
      <el-sub-menu v-for="item in hasChildren" :key="item.path" :index="item.path">
        <template #title>
          <!-- 动态获取icon图标 -->
          <component :is="item.icon" class="icons"></component>
          <!-- 显示文字内容 -->
          <span>{{ item.label }}</span>
        </template>
        <!-- 分组 -->
        <el-menu-item-group>
          <!-- 循环遍历子菜单 -->
          <el-menu-item @click="handleMenu(child)" v-for="child in item.children" :key="child.path"
                        :index="child.path">
            <!-- 动态获取icon图标 -->
            <component :is="child.icon" class="icons"></component>
            <!-- 显示文字内容 -->
            <span>{{ child.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<script setup>
import { ref, computed } from 'vue'
import { storeToRefs } from 'pinia';
import { useAllDateStore } from '@/stores/index';
// 获取 store 实例
const store = useAllDateStore();
// 使用 storeToRefs 解构响应式属性
const { isCollapse } = storeToRefs(store);
// 根据 isCollapse 来计算宽度
const width = computed(() => isCollapse.value ? '64px' : '200px')

const list = ref([
  {
    path: '/home',
    name: 'home',
    label: '首页',
    icon: 'house',
    url: 'Home'
  },
  {
    path: '/mall',
    name: 'mall',
    label: '商品管理',
    icon: 'video-play',
    url: 'Mall'
  },
  {
    path: '/user',
    name: 'user',
    label: '用户管理',
    icon: 'user',
    url: 'User'
  },
  {
    path: 'other',
    label: '其他',
    icon: 'location',
    children: [
      {
        path: '/page1',
        name: 'page1',
        label: '页面1',
        icon: 'setting',
        url: 'Page1'
      },
      {
        path: '/page2',
        name: 'page2',
        label: '页面2',
        icon: 'setting',
        url: 'Page2'
      }
    ]
  }
])
const noChildren = computed(() => list.value.filter(item => !item.children))
const hasChildren = computed(() => list.value.filter(item => item.children))

// 路由
import { useRouter, useRoute } from 'vue-router';
const router = useRouter()
// handleMenu方法的实现
const handleMenu = (item) => {
  router.push(item.path)
}
</script>

<style lang="less" scoped>
.icons {
  width: 18px;
  height: 18px;
  margin-right: 5px;
}

.el-menu {
  border-right: none;

  h3 {
    line-height: 48px;
    color: #fff;
    text-align: center;
  }
}

.el-aside {
  height: 100%;
  background-color: #545c64;
}
</style>